<?php
namespace app\admin\model\dealer;
use app\common\enum\OrderType as OrderTypeEnum;
use app\common\service\Order as OrderService;
use think\Model;

class DealerOrder extends Model
{

    protected $name = 'dealer_order';

    /**
     * 订单类型
     * @param $value
     * @return array
     */
    public function getOrderTypeAttr($value)
    {
        $types = OrderTypeEnum::getTypeName();
        return ['text' => $types[$value], 'value' => $value];
    }

    public function wxapp(){
        return $this->belongsTo("app\admin\model\wxapp\Wxapp",'wxapp_id','wxapp_id');
    }

    /**
     * 订单所属用户
     * @return \think\model\relation\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('app\admin\model\user\User');
    }

    /**
     * 一级分销商用户
     * @return \think\model\relation\BelongsTo
     */
    public function dealerFirst()
    {
        return $this->belongsTo('DealerUser', 'first_user_id');
    }

    /**
     * 二级分销商用户
     * @return \think\model\relation\BelongsTo
     */
    public function dealerSecond()
    {
        return $this->belongsTo('DealerUser', 'second_user_id');
    }

    /**
     * 三级分销商用户
     * @return \think\model\relation\BelongsTo
     */
    public function dealerThird()
    {
        return $this->belongsTo('DealerUser', 'third_user_id');
    }


    /**
     * 订单详情
     * @param $where
     * @return Order|null
     * @throws \think\exception\DbException
     */
    public static function detail($where)
    {
        return static::get($where);
    }

    /**
     * 订单详情
     * @param $orderId
     * @param $orderType
     * @return Order|null
     * @throws \think\exception\DbException
     */
    public static function getDetailByOrderId($orderId, $orderType)
    {
        return static::detail(['order_id' => $orderId, 'order_type' => $orderType]);
    }
    /**
     * 订单商品列表
     * @return \think\model\relation\HasMany
     */
    public function goods()
    {
        $module = self::getCalledModule() ?: 'common';
        return $this->hasMany("app\\{$module}\\model\\vipcard\\Card");
    }


    /**
     * 分红开关状态
     */
    public  function getOrderTypeList(){
        return  [ 10 => '商城订单', 30 => '储值卡订单', 40 => '购买爆品礼', 50 => '当面付'];
    }

    /**
     * 零钱开关状态
     */
    public  function getSettledList(){
        return  [ 0 => '未结算', 1 => '已结算'];
    }


    /**
     * 获取分销商订单列表
     * @param null $user_id
     * @param int $is_settled
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($query = [])
    {

        // 检索查询条件
        !empty($query) && $this->setWhere($query);
        // 获取分销商订单列表
        $data= $this->with([
            'dealer_first.user',
            'dealer_second.user',
            'dealer_third.user',
            'wxapp'
        ])->alias('order')->field('order.*,wxapp.wxapp_id,wxapp.wxapp_name')
            ->join('wxapp', 'wxapp.wxapp_id = order.wxapp_id')
            ->order(['order.create_time' => 'desc'])
            ->paginate(10, false, [
                'query' => \request()->request()
            ]);

        if ($data->isEmpty()) {
            return $data;
        }

        // 获取订单的主信息
        $with = ['goods' => ['image', 'refund'], 'address', 'user'];
        $with2 = ['orderplan' => ['image'], 'address', 'user'];
        $with3 = ['goods' => ['image'], 'user'];
        $with4 = ['user'];
        return OrderService::getOrderList($data, 'order_master', $with,$with2,$with3,$with4);
    }


    /**
     * 设置检索查询条件
     * @param $query
     */
    private function setWhere($query)
    {

        if (isset($query['search']) && !empty($query['search'])) {
            $this->where('wxapp.wxapp_name', 'like', '%' . trim($query['search']) . '%');
        }
        if (isset($query['order_status']) && !empty($query['order_status'])) {
            $query['order_status'] > -1 && $this->where('order.is_settled', '=', $query['order_status']);
        }
        if (isset($query['order_type']) && $query['order_type'] > 0) {
            $this->where('order.order_type', '=', (int)$query['order_type']);
        }
        if (isset($query['search_time']) && !empty($query['search_time'])) {
            $times=explode('至',$query['search_time']);
            $this->where('order.create_time','between time',$times);
        }

    }

}